package com.itcoon.transform.base.init;

import com.itcoon.transform.base.pool.AssembleBeanHolder;
import com.itcoon.transform.base.register.annotated.AnnotatedAssembleModelRegisterComposite;
import com.itcoon.transform.base.definition.ModelDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.Duration;
import java.time.Instant;
import java.util.Set;

public class AnnotatedModelDefinitionInitializer implements InitializingBean {

    private static final Logger log = LoggerFactory.getLogger(ModelDefinition.class);

    @Autowired
    private AnnotatedAssembleModelRegisterComposite annotatedAssembleModelRegisterComposite;

    @Override
    public void afterPropertiesSet() {
        AssembleBeanHolder assembleBeanHolder = AssembleBeanHolder.getInstance();
        Set<Class<?>> targetClasses = assembleBeanHolder.getAllTargetClass();
        Instant start = Instant.now();
        for (Class<?> targetClass : targetClasses) {
            ModelDefinition md = annotatedAssembleModelRegisterComposite.register(targetClass);
            assembleBeanHolder.registerModelDefinition(md);
            log.debug("[Transform4j] - Loaded dtoModelDefinition successfully for targetClass - {}", targetClass.getSimpleName());
        }
        log.info("[Transform4j] - Loaded total {} annotated targetClasses, spend {} ms", targetClasses.size(), Duration.between(start, Instant.now()).toMillis());
    }
}
